<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="http://at.alicdn.com/t/font_1309180_m0vigzfu7y.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <link rel="stylesheet" href="css/superadmin_dashboard.css">
    <title>用户和管理员管理</title>
    <style>
        body {
            background-image: url('https://images.pexels.com/photos/1037992/pexels-photo-1037992.jpeg?auto=compress&cs=tinysrgb&w=800');
            background-size: cover;
            background-attachment: fixed;
            color: #333;
            font-family: Arial, sans-serif;
        }
        nav ul {
            list-style-type: none;
            padding: 0;
            display: flex;
            justify-content: space-around;
            background-color: rgba(255, 255, 255, 0.8); /* 透明白色背景 */
            padding: 10px 0;
            border-radius: 10px;
        }
        nav ul li {
            display: inline;
            margin-right: 10px;
        }
        nav ul li a {
            color: #333;
            text-decoration: none;
            padding: 10px 20px;
            border-radius: 5px;
            transition: background-color 0.3s;
        }
        nav ul li a:hover {
            background-color: rgba(0, 0, 0, 0.1);
        }
        nav .search-bar {
            display: flex;
            align-items: center;
        }
        nav .search-bar input {
            border-radius: 5px 0 0 5px;
            border: none;
            padding: 10px;
        }
        nav .search-bar button {
            border-radius: 0 5px 5px 0;
            border: none;
            padding: 10px;
            background-color: #007BFF;
            color: #fff;
            cursor: pointer;
        }
        .content {
            display: flex;
            margin: 20px;
            background-color: rgba(255, 255, 255, 0.8); /* 透明白色背景 */
            padding: 20px;
            border-radius: 10px;
        }
        .sidebar {
            width: 200px;
            margin-right: 20px;
        }
        .sidebar h2 {
            color: #000;
        }
        .sidebar button {
            display: block;
            width: 100%;
            padding: 10px;
            margin: 10px 0;
            cursor: pointer;
            background-color: #007BFF;
            color: white;
            border: none;
            border-radius: 5px;
            transition: background-color 0.3s;
        }
        .sidebar button:hover {
            background-color: #0056b3;
        }
        .main-content {
            flex-grow: 1;
        }
        .main-content button {
            padding: 10px 20px;
            margin: 10px;
            cursor: pointer;
            background-color: #007BFF;
            color: white;
            border: none;
            border-radius: 5px;
            transition: background-color 0.3s;
        }
        .main-content button:hover {
            background-color: #0056b3;
        }
        #user-info {
            margin-top: 20px;
            background-color: rgba(255, 255, 255, 0.8); /* 透明白色背景 */
            padding: 20px;
            border-radius: 10px;
            overflow: auto;
        }
    </style>
</head>
<body>
    <!-- 导航栏 -->
    <nav>
        <ul>
            <li>
                <a href="dashboard.html"> <i class="fas fa-home"></i> 返回</a>
            </li>
            <li class="search-bar">
                <input type="text" id="searchInput" placeholder="搜索...">
                <button onclick="search()"><i class="iconfont iconchazhao"></i></button>
            </li>
            <li><a href="https://www.taobao.com" target="_blank">编者推荐</a></li>
            <li><a href="https://yiyan.baidu.com/" target="_blank">AI导航</a></li>
        </ul>
    </nav>
    <div class="content">
        <div class="sidebar">
            <div id="user-management">
                <h2>用户管理</h2>
                <button id="add-user">增加用户</button>
                <button id="delete-user">删除用户</button>
            </div>
            <div id="admin-management">
                <h2>管理员管理</h2>
                <button id="add-admin">增加管理员</button>
                <button id="delete-admin">删除管理员</button>
            </div>
        </div>
        <div class="main-content">
            <div>
                <button id="user-btn">用户</button>
                <button id="admin-btn">管理员</button>
            </div>
            <div id="user-info"></div>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const token = localStorage.getItem('access_token');
            const userId = localStorage.getItem('user_id');
            let managerData = {};
            let userData = {};

            // 获取所有用户和管理员信息
            function fetchUserInfo() {
                fetch('http://192.168.78.248:5000/super_ma_user', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'Authorization': `Bearer ${token}`
                    },
                    body: JSON.stringify({ user_id: userId })
                })
                .then(response => response.json())
                .then(data => {
                    managerData = data.manager;
                    userData = data.user;
                    displayUserInfo('user');
                })
                .catch(error => console.error('Error:', error));
            }

            // 展示用户和管理员信息
            function displayUserInfo(type) {
                const userInfoDiv = document.getElementById('user-info');
                if (type === 'user') {
                    userInfoDiv.innerHTML = '<pre>' + JSON.stringify(userData, null, 2) + '</pre>';
                } else {
                    userInfoDiv.innerHTML = '<pre>' + JSON.stringify(managerData, null, 2) + '</pre>';
                }
            }

            // 增加或删除用户/管理员
            function modifyUser(action, code) {
                const idBeDone = prompt("请输入用户或管理员ID:");
                if (action === 'add') {
                    const userPassword = prompt("请输入用户密码:");
                    const name = prompt("请输入姓名:");
                    const email = prompt("请输入邮箱:");
                    var data = {
                        user_id: userId,
                        code: code,
                        id_be_done: idBeDone,
                        user_password: userPassword,
                        name: name,
                        email: email
                    };
                } else {
                    var data = {
                        user_id: userId,
                        code: code,
                        id_be_done: idBeDone
                    };
                }

                fetch('http://192.168.78.248:5000/super_ma_user', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'Authorization': `Bearer ${token}`
                    },
                    body: JSON.stringify(data)
                })
                .then(response => response.json())
                .then(data => {
                    alert('操作成功');
                    fetchUserInfo();
                })
                .catch(error => console.error('Error:', error));
            }

            // 页面加载时获取用户和管理员信息
            fetchUserInfo();

            // 绑定按钮事件
            document.getElementById('add-user').addEventListener('click', () => modifyUser('add', 0));
            document.getElementById('delete-user').addEventListener('click', () => modifyUser('delete', 0));
            document.getElementById('add-admin').addEventListener('click', () => modifyUser('add', 1));
            document.getElementById('delete-admin').addEventListener('click', () => modifyUser('delete', 1));
            document.getElementById('user-btn').addEventListener('click', () => displayUserInfo('user'));
            document.getElementById('admin-btn').addEventListener('click', () => displayUserInfo('manager'));
        });

        function search() {
            const searchInput = document.getElementById('searchInput').value;
            const userId = localStorage.getItem('user_id') || 'guest';
            const token = localStorage.getItem('access_token');

            if (searchInput.trim() === '') {
                alert('请输入搜索内容');
                return;
            }

            const requestData = {
                user_id: parseInt(userId),
                search: searchInput
            };

            fetch('http://192.168.78.248:5000/search', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'Authorization': `Bearer ${token}`
                },
                body: JSON.stringify(requestData)
            })
            .then(response => response.json())
            .then(data => {
                if (data.status === 'successfully') {
                    localStorage.setItem('searchResults', JSON.stringify(data.film));
                    window.location.href = 'search.html';
                } else {
                    alert('暂无搜索结果');
                }
            })
            .catch(error => {
                console.error('Error:', error);
            });
        }
    </script>
</body>
</html>
